package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.LoanBidsDetail;
import com.gxa.p2p.service.LoanReviewService;
import com.gxa.p2p.util.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author BKP
 * @date 2020.8.6
 */
@CrossOrigin
@RestController
@RequestMapping("/loanReview")
@Api(value = "借款审核的控制器", tags = "借款审核模块")
public class LoanReviewController {

    @Resource(name = "loanReviewService")
    LoanReviewService loanReviewService;

    /**
     * 查询所有数据 可带查询条件 且带分页条件
     * @param paramMap
     * @return Map
     */
    @PostMapping
    @ApiOperation(value = "查询所有数据的接口,可输入其他查询条件", response = Map.class, httpMethod = "POST")
    public Map<String,Object> findAll(@RequestBody @ApiParam(value = "参数map,包含状态,时间范围,分页参数", required = true, type = "Map") Map<String,Object> paramMap) {
        MapUtil mapUtil = new MapUtil();
        List<LoanBidsDetail> list = loanReviewService.findAll(paramMap);
        Integer dataSum = loanReviewService.dataSum(paramMap);
        if (dataSum > 0) {
            mapUtil.setCode(0);
            mapUtil.put("data",list);
            mapUtil.put("count",dataSum);
            return mapUtil.getMap();
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
    }

    /**
     * 追加审核信息(改) 添加到该条数据上
     * @param loanBidsDetail
     * @return Map
     */
    @PutMapping
    @ApiOperation(value = "追加审核信息的接口", response = Map.class, httpMethod = "PUT")
    public Map<String,Object> updateReviewStatus(@RequestBody @ApiParam(value = "借款审核对象,包含该条审核的所有信息", required = true, type = "LoanBidsDetail") LoanBidsDetail loanBidsDetail) {
        MapUtil mapUtil = new MapUtil();
        Integer lines = loanReviewService.updateReviewStatus(loanBidsDetail);
        if (lines > 0)
            mapUtil.setCode(0);
        else
            throw new BusinessException(ErrorCode.FAILED);
        return mapUtil.getMap();
    }
}
